function playName(a) {
    var b = new Array();
    return b["youku"] = "优酷", b["iqiyi"] = "爱奇艺", b["letv"] = "乐视", b["sohu"] = "搜狐", b["pptv"] = "聚力", b["qq"] = "腾讯", b["bilibili"] = "哔哩哔哩", b["acfun"] = "A站", b["kankan"] = "天天看看", b["mgtv"] = "芒果TV", b["pan"] = "百度云", b["yun"] = "百度云", b["other"] = "α", b["otherB"] = "β", b[a];
}
function history(obj){
    if(PlayHistoryObj){
        var json = {
            'vod_id' : obj.id ,
            'vod_pid': obj.pid,
            'vod_sid': obj.sid,
            'vod_name' : obj.name,
            'url_name' : obj.urlName,
        };
        PlayHistoryObj.addPlayHistory(json,obj.url,window.location.href);
    }
}
function isMobile(){
    var ua = navigator.userAgent;
    var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
        isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
        isAndroid = ua.match(/(Android)\s+([\d.]+)/),
        isMobile = isIphone || isAndroid;
    if(isMobile) {
        return true
    }else{
        return false
    }
}

function m3u8(pv){
    return pv.indexOf('.mp4') !== -1 || pv.indexOf('.m3u8') !== -1;
}

function isHtml(pv) {
    return pv.indexOf('.html') !== -1 || pv.indexOf('.shtml') !== -1 || pv.indexOf('.htm') !== -1 || pv.indexOf('https://') !== -1 || pv.indexOf('http://') !== -1;
}

var player = {
    isJump: function (pv, playname) {
        var url = ''
        switch (playname) {
            case 'youku':
                url = this.youku(pv);
                break;
            case 'tudou':
                url = this.tudou(pv);
                break;
            case 'iqiyi':
                url = this.iqiyi(pv);
                break;
            case 'viqiyi':
                url = this.iqiyi(pv);
                break;
            case 'letv':
                url = this.letv(pv);
                break;
            case 'sohu':
                url = this.sohu(pv);
                break;
            case 'pptv':
                url = this.pptv(pv);
                break;
            case 'qq':
                url = this.qq(pv);
                break;
            case 'bilibili':
                url = this.bilibili(pv);
                break;
            case 'acfun':
                url = this.acfun(pv);
                break;
        }
        return url
    },
    ykUrl: function (data) {
        return 'https://v.youku.com/v_show/id_' + data + '.html';
    },
    tudou: function (pv) {
        var data = pv.split(',');
        var len = data.length;
        if (len === 1) {
            this.youku(pv);
        } else if (len === 2) {
            this.youku('/');
        } else if (len >= 3) {
            this.youku(data[2]);
        }
    },
    youku: function (pv) {
        var data = pv.split(',');
        return pv === '/' ? '/' : this.ykUrl(data.length === 3 ? data[2] : pv);
    },
    iqiyi: function (pv) {
        var plus = isMobile() ? '&tvid=' : '&tvId=';
        var data = [];
        if (pv.indexOf(",") > 0) {
            data = pv.split(',');
            vid = data[1] + plus + data[0];
            pid = data[0];
        } else if (pv.indexOf("&tvid=") > 0) {
            data = pv.split('&tvid=');
            vid = data[0] + plus + data[1];
            pid = data[1];
        } else if (pv.indexOf("_") > 0) {
            data = pv.split('_');
            vid = data[1] + plus + data[0];
            pid = data[0];
        }
        if (isMobile()) {
            purl = '//m.iqiyi.com/shareplay.html?vid=' + vid + '&coop=coop_117_9949&cid=qc_105102_300452&bd=1&autoplay=1&fullscreen=1';
        } else {
            purl = '//open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=' + vid;
        }
        return purl;
    },
    letv: function (pv) {
        var data = pv.split(',');
        var len = data.length;
        var url = '//www.le.com/ptv/vplay/' + data[0] + '.html';
        if (len === 2) {
            url = '/'
        }
        return url;
    },
    sohu: function (pv) {
        return '//tv.sohu.com/upload/static/share/share_play.html#' + pv.split('_')[0] + (isMobile() ? '' : '_9468532_0_9001_0')
    },
    pptv: function (pv) {
        return '//' + (isMobile() ? 'm' : 'www') + '.pptv.com/show/' + pv.split(',')[0] + '.html'
    },
    qq: function (pv) {
        return '//v.qq.com/iframe/player.html?vid=' + pv + '&tiny=0&auto=1'
    },
    bilibili: function (pv) {
        var data = pv.split(',');
        return pv.indexOf('http') !== -1 ? pv : (data.length === 2 ? '//www.bilibili.com/video/av' + data[0] + '/?p=' + data[1] : '//www.bilibili.com/video/av' + pv + '/');
    },
    acfun: function (pv) {
        var vid = '';
        var data = []
        if (pv.indexOf('ab') != -1) {
            data = pv.split('ab');
            var ab = data[1].split(',')
            if (ab.length === 2) {
                vid = ab[0] + '_' + ab[1];
            } else {
                vid = data[1];
            }
        } else {
            data = pv.split(',');
            var len = data.length;
            if (len == 2) {
                vid = data[0] + '_' + data[1];
            } else {
                vid = pv;
            }
        }
        return isMobile() ? '//m.acfun.cn/v/?' + (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + '=' + vid : '//www.acfun.cn/v/' + (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + vid
    }
}

function play(pv, name){
    var data = []
    if (pv.indexOf('@@') !== -1) {
        data = pv.split('@@');
        playname = data[1];
        pv = data[0];
    }
    if(pv.indexOf('ikanfan.cn') !== -1) {
        pv = pv.split('=')[1].split('&')[0];
        if(m3u8(pv)){
            return '/';
        } else if(isHtml(pv)) {
            return pv;
        }
    } else if(m3u8(pv)){
        return '/';
    } else if(isHtml(pv)) {
        return pv;
    } else {
        return player.isJump(pv, name)
    }
}

//附加参数
function one(data){
    var d = data.split(',');
    var text = '';
    text += d.indexOf('1') != -1 ? '' : '<span class="online">在线</span>';
    text += d.indexOf('2') != -1 ? '<span class="noad">无广告</span>' : d.indexOf('3') != -1 ? '' : '<span class="ad">有广告</span>';
    for(var i = 0; i < d.length; i++){
        var c = parseInt(d[i]);
        if(c === 3){
            text += '<span class="vip">会员</span>';
        }else if(c === 4){
            text += '<span class="down">下载</span>';
        }else if(c === 5){
            text += '<span class="pan">网盘</span>';
        }else if(c === 6){
            text += '<span class="torrent">种子</span>';
        }else if(c === 7){
            text += '<span class="magnet">磁力</span>';
        }else if(c === 8){
            text += '<span class="no">无修</span>';
        }else if(c === 9){
            text += '<span class="hd">生肉</span>';
        }else if(c === 10){
            text += '<span class="hd">1080P</span>';
        }else if(c === 11){
            text += '<span class="hd">720P</span>';
        }else if(c === 12){
            text += '<span class="hd">480P</span>';
        }else if(c === 13){
            text += '<span class="hd">TS</span>';
        }
    }
    return text;
}

$(document).ready(function(){
    var url = '//www.99496.com/index.php?s=home-vod-playerJson-id-' + playConfig.id;
    var target = isMobile() ? '_self' : '_blank';
    $.getJSON(url, function(data){
        if(data.status === 0){
            var data = data.data.Data, html = '', vid, name, type;
            for (var e = 0; e < data.length; e++) {
                var d = data[e].playurls[parseInt(playConfig.pid) - 1]
                vid = playConfig.pid <= data[e].playurls.length && d[1];
                if(vid){
                    type = data[e].playname;
                    name = playName(type);
                    html += '<a target="'+ target + '" class="bf" href="' + play(vid, type) + '"><span class="fr">' + one(type === 'bilibili' || type === 'acfun' ? '2' : d[3])  + '</span><i class="'+ type +'"></i>' + (name || d[4] || '其他') +'</a>';
                }
            }
            $(".playerbox .playlist").html(html);
        }
    });
    /*调用方法如下：*/
    var taBox = $(".tab_conbox a");
    if(taBox.length > 100){
        $.jqtab = function(tabtit,tab_conbox,shijian,zindex) {
            $(tab_conbox).find("li").hide();
            $(tabtit).find("li:eq("+zindex+")").addClass("active").show();
            $(tab_conbox).find("li:eq("+zindex+")").show();
            $(tabtit).find("li").bind(shijian,function(){
                $(this).addClass("active").siblings("li").removeClass("active");
                var activeindex = $(tabtit).find("li").index(this);
                $(tab_conbox).children().eq(activeindex).show().siblings().hide();
                return false;
            });
        };
        var jTatal = parseInt(window.location.pathname.split('p/')[1].split('/')[2] || 1);
        var jLen = Math.floor(taBox.length/100+1);
        var zindex = jTatal%100 === 0 ? Math.floor(jTatal/100) - 1 : Math.floor(jTatal/100);
        for(i=1;i<=jLen;i++){
            var jstart = (i-1)*100+1;
            var jend = (i-1)*100+100;
            $(".ep-page").append('<li>'+jstart+'-'+jend+'</li>');
        }
        taBox.each(function(i){
            taBox.slice(i*100,i*100+100).wrapAll('<li class="tab_con"></li>');
        });
        $.jqtab("#ep-page","#tab_conbox","click",zindex);
    }
});
